﻿using BLL;
using EncryptHelper;
using MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.SessionState;

namespace Loginter.ajax
{
    /// <summary>
    /// CarriesAdd 的摘要说明
    /// </summary>
    public class CarriesAdd : IHttpHandler,IReadOnlySessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //获取添加的驾驶员信息
            string username = context.Request.Form["UserName"];
            string sex = context.Request.Form["Sex"];
            string datetime = context.Request.Form["DateTime"];
            string phone = context.Request.Form["Phone"];
            string number = context.Request.Form["Number"];
            string team = context.Request.Form["Team"];
            string state = context.Request.Form["State"];
            string remark = context.Request.Form["Remark"];
            string addtime = context.Request.Form["AddTime"];
            //判断车队id
            if (!Regex.IsMatch(team, @"^[0-9]*[1-9][0-9]*$"))
            {
                context.Response.Write("您输入的车队ID不正确，id为正整数！");
                return;
            }
            //判断电话号码格式
            if (!Regex.IsMatch(phone, @"^1[35789]\d{9}$"))
            {
                context.Response.Write("您输入的手机号码格式不正确");
                return;
            }
            //判断身份证号格式 身份证号码
            // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X 
            if (!Regex.IsMatch(number, @"^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$"))
            {
                context.Response.Write("您输入的身份证号码格式不正确");
            }
            //判断时间格式 生日
            if (!Regex.IsMatch(datetime, @"^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$"))
            {
                context.Response.Write("您输入的生日格式不正确");
                return;
            }
            //判断时间格式 添加时间
            if (!Regex.IsMatch(addtime, @"^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$"))
            {
                context.Response.Write("您输入的添加时间格式不正确");
                return;
            }
            DriverBLL driverBLL = new DriverBLL();
            DriverMODEL model = new DriverMODEL()
            {
                Name = username,
                //Sex = Convert.ToInt32(sex),
                //Brith = Convert.ToDateTime(datetime),
                //Phone = phone,
                //IDCard =number,
                //FK_TeamID = Convert.ToInt32(team),
                //State = Convert.ToInt32(state),
                //Remark = remark,
                //CheckInTime = Convert.ToDateTime(addtime)
            };
            int result = driverBLL.Getdriver_add(model);
            JavaScriptSerializer js = new JavaScriptSerializer();
            if (result==1)
            {
                context.Session["CarriesInfo"] = model.DriverID;
                //context.Response.Write("yes");
                OperateResult usert = new OperateResult(1, "添加成功");
                string json = js.Serialize(usert);
                context.Response.Write(json);
            }
            else
            {
                OperateResult usert = new OperateResult(1, "添加失败");
                string json = js.Serialize(usert);
                context.Response.Write(json);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}